package com.web.manager.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.manager.pojo.Coursesection;
import com.web.manager.pojo.Result;
import com.web.manager.service.CoursesectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin  //解决跨域问题
@RestController
@RequestMapping("/section")
public class CoursesectionController {
    @Autowired
    private CoursesectionService coursesectionService;

    @GetMapping  //简写 //只限get方式可用以请求
    public Result list(){

        //调用service查询部门数据
        List<Coursesection> deptList = coursesectionService.list();
        return Result.success(deptList);

    }

    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody Coursesection type){
        coursesectionService.save(type);
        return Result.success();
    }

    //修改
    @PutMapping("/update")
    public Result update(@RequestBody Coursesection type){
        coursesectionService.updateById(type);  //updateUser
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){

        coursesectionService.removeById(id);  //deleteUser
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){

        coursesectionService.removeBatchByIds(ids);   //batchDeleteUser
        return Result.success();
    }
    /**
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String  name) {

        QueryWrapper<Coursesection> queryWrapper = new QueryWrapper<>();  //条件构造器
        queryWrapper.like(StrUtil.isNotBlank(name),"name",name);
        //select * from manager_user where username like '%#{username}% and name like '%#{name}%'

        Page<Coursesection> page = coursesectionService.page(new Page<>(pageNum,pageSize), queryWrapper);        //selectByPage
        return Result.success(page);
    }

}
